package com.sch.study.concurrency;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * User: hui_stone
 * Date: 2016/2/14 0014
 * Time: 上午 9:19
 */
public class SemaphoreTest {

    private static final int THREAD_COUNT = 30;

    private static ExecutorService threadpool = Executors.newFixedThreadPool(THREAD_COUNT);

    private static Semaphore semaphore = new Semaphore(10);

    public static void main(String[] args) {

        for(int i =0;i<THREAD_COUNT;i++){
            threadpool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        System.out.println("线程：" + Thread.currentThread().getName() + "正在连接数据库...");
                        Thread.sleep(3000);
                        semaphore.release();
                        System.out.println("线程："+Thread.currentThread().getName()+"处理完毕...");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        threadpool.shutdown();
        //System.out.println("......全部结束....game over!");
    }
}
